from django.db import models
import time
from django.contrib.auth.models import AbstractUser

# Create your models here.
  
class Image(models.Model):  
    title = models.CharField(max_length=100)  
    image = models.ImageField(upload_to='images/')  
    uploaded_at = models.DateTimeField(auto_now_add=True)  
  
    def __str__(self):  
        return self.title
		
class Submission(models.Model):  
    location = models.CharField(max_length=255)  # 点位  
    issue = models.CharField(max_length=255)     # 问题  
    timestamp = models.DateTimeField(auto_now_add=True)  # 时间（自动设置为提交时间）  
    type = models.CharField(max_length=50)       # 类型  
    description = models.TextField()             # 描述  
  
    def __str__(self):  
        return self.location + " - " + self.issue
		
		
class RelatedForm(models.Model):  
    original_form = models.OneToOneField(Submission, on_delete=models.CASCADE, related_name='related_form')  
    progress = models.CharField(max_length=50) 
    progress_description = models.TextField()	
    # 可以选择性地再次包含location, issue, description字段，  
    # 但通常这些字段可以通过original_form关联查询得到，除非你有特殊需求。  
 
    def __str__(self):  
        return f"Related form for {self.original_form.location}"
		
		
#用户
class CustomUser(AbstractUser):
    username = models.CharField(max_length=150, unique=True)
    company = models.CharField(max_length=255)
    department = models.CharField(max_length=255)
    position = models.CharField(max_length=255)
    custom_id = models.CharField(max_length=255)

    groups = models.ManyToManyField(
        'auth.Group',
        verbose_name='groups',
        blank=True,
        help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.',
        related_name="customuser_groups",  # 指定一个唯一的 related_name
        related_query_name="user",  # 可选，用于反向查询时的名称
    )
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        verbose_name='user permissions',
        blank=True,
        help_text='Specific permissions for this user.',
        related_name="customuser_user_permissions",  # 指定一个唯一的 related_name
        related_query_name="user",  # 可选，用于反向查询时的名称
    )

    def save(self, *args, **kwargs):
        if not self.custom_id:
            self.custom_id = str(int(round(time.time() * 1000)))
            self.id = self.custom_id
            #self.is_active = True
        super().save(*args, **kwargs)

class Task(models.Model):
    task_id = models.CharField(max_length=255, unique=True)
    created_by = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='created_tasks')
	
    def save(self, *args, **kwargs):
        if not self.task_id:
            self.task_id = str(int(round(time.time() * 1000)))
            self.id = self.task_id
        super().save(*args, **kwargs)

class SubTask(models.Model):
    subtask_id = models.CharField(max_length=255, unique=True)
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='subtasks')
    responsible_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='responsible_subtasks')
    created_by = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='created_subtasks')
    description = models.TextField()
    progress = models.IntegerField(default=0)
    importance = models.CharField(max_length=50, choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High')])
	
    def save(self, *args, **kwargs):
        if not self.subtask_id:
            self.subtask_id = str(int(round(time.time() * 1000)))
            self.id = self.subtask_id
        super().save(*args, **kwargs)

class AttachedTask(models.Model):
    attached_task_id = models.CharField(max_length=255, unique=True)
    subtask = models.ForeignKey(SubTask, on_delete=models.CASCADE, related_name='attached_tasks')
    responsible_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='responsible_attached_tasks')
    description = models.TextField()
    progress = models.IntegerField(default=0)
    importance = models.CharField(max_length=50, choices=[('low', 'Low'), ('medium', 'Medium'), ('high', 'High')])
	
    def save(self, *args, **kwargs):
        if not self.attached_task_id:
            self.attached_task_id = str(int(round(time.time() * 1000)))
            self.id = self.attached_task_id
        super().save(*args, **kwargs)

class Message(models.Model):
    sender = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='sent_messages')
    receiver = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='received_messages')
    text = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to='messages/images/', blank=True, null=True)
    file = models.FileField(upload_to='messages/files/', blank=True, null=True)
    audio = models.FileField(upload_to='messages/audio/', blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)
	
	
	